package net.wangds.auth.module.impls.defaults;

import lombok.*;
import lombok.experimental.SuperBuilder;
import net.wangds.auth.module.DisabledResource;
import net.wangds.auth.module.ResourceSchemaEnum;
import net.wangds.data.DisableEnum;
import net.wangds.data.VisibilityEnum;
import net.wangds.data.impl.IdCreate;
import net.wangds.log.helper.LogHelper;
import org.apache.commons.lang3.StringUtils;

import javax.persistence.*;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;


@NoArgsConstructor
@AllArgsConstructor
@SuperBuilder
@MappedSuperclass
public class DefaultResource  extends IdCreate implements DisabledResource {

    private static final long serialVersionUID = 0;
    /**
     * 权限域.
     */
    @Setter
    @Getter
    @Enumerated(EnumType.STRING)
    @Column(name="protocol")
    private ResourceSchemaEnum schema;

    /**
     * 系统.
     */
    @Setter
    @Getter
    @Column(length=50)
    private String sys;


    /**
     * 模块.
     */
    @Setter
    @Getter
    @Column(length=50)
    private String module;

    /**
     * 特性.
     */
    @Setter
    @Getter
    @Column(length=50)
    private String feature;

    /**
     * 活动.
     */
    @Setter
    @Getter
    @Column(length=50)
    private String activity;


    /**
     * 权限显示名称.
     */
    @Setter
    @Getter
    @Column(length=50)
    private String resourceLabel;

    /**
     * 描述.
     */
    @Setter
    @Getter
    @Lob
    @Column(columnDefinition = "text")
    private String resourceDesc;

    @Setter
    @Getter
    @Enumerated(EnumType.ORDINAL)
    @Column
    @Builder.Default
    private DisableEnum disable = DisableEnum.NORMAL;

    @Setter
    @Getter
    @Builder.Default
    @Column
    private long orderWeight=0;

    @Setter
    @Getter
    @Builder.Default
    @Column
    private VisibilityEnum visible = VisibilityEnum.VISIBLE;


    @Override
    public URL toUrl(){
        try {
            StringBuilder sb = new StringBuilder();
            sb.append(schema).append("://");


            if(StringUtils.isNotBlank(sys)){
                sb.append("/").append(sys);
            }else{
                sb.append("/");
            }

            if(StringUtils.isNotBlank(module)){
                sb.append("/").append(module);
            }else{
                sb.append("/");
            }

            if(StringUtils.isNotBlank(feature)){
                sb.append("/").append(feature);
            }else{
                sb.append("/");
            }

            if(StringUtils.isNotBlank(activity)){
                try {
                    sb.append("/").append(URLEncoder.encode(activity, "UTF8"));
                } catch (UnsupportedEncodingException e) {
                    LogHelper.error(e);
                }
            }else{
                sb.append("/");
            }

            return new URL(sb.toString());
        } catch (MalformedURLException e) {
            LogHelper.error(e);
        }
        return null;
    }

}
